package com.wzxy.medical_backed.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzxy.medical_backed.entity.Policy;
import com.wzxy.medical_backed.entity.Result;
import com.wzxy.medical_backed.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    @GetMapping("/getall")
    public Result getAll(@RequestParam(required = false) String keyword,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer size) {
        QueryWrapper<Policy> queryWrapper = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.like("year", keyword);
        }
        IPage<Policy> policyPage = policyService.page(new Page<>(page, size), queryWrapper);
        return new Result("查询成功", policyPage, 200);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Policy policy) {
        boolean saved = policyService.save(policy);
        return saved ? new Result("添加成功", null, 200) : new Result("添加失败", null, 500);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Policy policy) {
        boolean updated = policyService.updateById(policy);
        return updated ? new Result("更新成功", null, 200) : new Result("更新失败", null, 500);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean removed = policyService.removeById(id);
        return removed ? new Result("删除成功", null, 200) : new Result("删除失败", null, 500);
    }
}